﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OVS.CoreDomain;
using OVS.CoreDomain.Data;
using OVS.CoreDomain.Business;

namespace OVS.Business
{
    public class BusinessElectionResults : IBusinessElectionResults
    {
        private readonly IBusinessEncryptVotes _businessEncryptVotes;
        private readonly IBusinessElection _businessElection;


        public BusinessElectionResults()
        {
            _businessEncryptVotes = new BusinessEncryptVotes();
            _businessElection = new BusinessElection();
        }

        public BusinessElectionResults(IBusinessEncryptVotes businessEncryptVotes)
        {
            _businessEncryptVotes = businessEncryptVotes;
        }

        public BusinessElectionResults(IBusinessElection businessElection)
        {
            _businessElection = businessElection;
        }




        #region IBusinessElectionResults Members


        public ICandidate GetWinner(List<ICandidate> candidatesDecrypted)
        {
            //_businessEncryptVotes.DecryptVotes(candidatesDecrypted);
            candidatesDecrypted.Sort(
                    delegate(ICandidate candidate1, ICandidate candidate2)
                    { return candidate1.Votes.CompareTo(candidate2.Votes); });
            return candidatesDecrypted.Last();
        }

        

        #endregion

        #region IBusinessElectionResults Members


        public bool ShowResults(IElection election)
        {
            bool _showResults = false;
            IElection _election =  _businessElection.GetElection(election.ElectionId);

            if (_election.ResultDate <= Convert.ToDateTime(DateTime.Now.ToShortDateString()))
                _showResults = true;

            return _showResults;

        }

        #endregion
    }
}
